<?php

namespace app\admin\controller;

use app\AdminBaseController;

class Node extends AdminBaseController {

    //节点树
    public function index(){
        if($this->request->isAjax()){
            $all = \app\admin\model\Menus::where('pid', '=', 0)->field('id,name as text,pid,icon,path')->select();
            foreach ($all as $item) {
                $item->state = 'closed';
                $map['pid'] = $item->id;
                $childNum = \app\admin\model\Menus::where($map)->count();
                if($childNum>0) {
                    $item->children = \app\admin\model\Menus::where($map)->field('id,name as text,pid,icon,path')->select();
                }
            }
            return json($all);
        } else {
            return view();
        }
    }

    // 左侧菜单列表
    public function getTree(){
        $admin = session('admin');
        if(is_null($admin)) return json(['code'=>-1,'message'=>'登录会话已过期']);
        $roleId = \app\admin\model\Admin::where('id','=',$admin['admin_id'])->value('role_id');

        if($roleId==1) {
            //所有权限
            $all = \app\admin\model\Menus::where('pid', '=', 0)->field('id,name as label,pid,icon,path')->select();
            foreach ($all as $item) {
                $where['is_menu'] = 1;
                $where['status'] = 1;
                $where['pid'] = $item->id;
                $childNum = \app\admin\model\Menus::where($where)->count();
                if ($childNum > 0) {
                    $item->children = \app\admin\model\Menus::where($where)->field('id,name as label,pid,icon,path')->select()->toArray();
                }
            }
            return json(['code' => 0, 'message' => '成功', 'data' => $all]);
        } else {
            // 其他角色权限
            $idx = \app\admin\model\AdminRoleAuth::where('role_id','=',$roleId)->value('menu_idx');
            $idArr = explode(',',$idx);
            $where['is_menu'] = 1;
            $where['status'] = 1;
            $where['pid'] = 0;
            $all = \app\admin\model\Menus::where($where)->whereIn('id',$idArr)->field('id,name as label,pid,icon,path')->select();
            foreach ($all as $item) {
                $map['is_menu'] = 1;
                $map['status'] = 1;
                $map['pid'] = $item->id;
                $childNum = \app\admin\model\Menus::where($map)->whereIn('id',$idArr)->count();
                if ($childNum > 0) {
                    $item->children = \app\admin\model\Menus::where($map)->whereIn('id',$idArr)->field('id,name as label,pid,icon,path')->select()->toArray();
                }
            }

            return json(['code' => 0, 'message' => '成功', 'data' => $all]);
        }
    }

    // 添加、编辑节点
    public function add(){
        if(!$this->request->isPost()) return json(['code'=>-1,'非法请求','data'=>[]]);
        $post = $this->request->post();
        if($post['id']>0){
            // 更新
            $find = \app\admin\model\Menus::where('id','=',$post['id'])->findOrEmpty();
            $find->name = trim($post['name']);
            $find->icon = trim($post['icon']);
            $find->sort_val = intval($post['sort_val']);
            $find->is_menu = intval($post['is_menu']);
            $find->status = intval($post['status']);
            $find->save();
        } else {
            $model = new \app\admin\model\Menus();
            $model->name = trim($post['name']);
            $model->pid = intval($post['pid']);
            $model->icon = trim($post['icon']);
            $model->path = trim($post['path']);
            $model->sort_val = intval($post['sort_val']);
            $model->is_menu = intval($post['is_menu']);
            $model->status =1;
            $model->created_at = date('Y-m-d H:i:s',time());
            $model->save();
        }
        return json(['code'=>0,'message'=>'操作成功','data'=>[]]);
    }

    // icons 列表
    public function iconList(){
        $rows = $this->request->param('rows',10,'intval');
        $name = $this->request->param('name','','trim');
        $where = [];
        if($name!=''){
            $where[] =['name','like','%'.$name.'%'];
        }
        $res = \app\model\Icons::where($where)->paginate($rows,false);
        return json(['total'=>$res->total(),'rows'=>$res->items()]);
    }

    //移除节点
    public function del(){
        if(!$this->request->isPost()) return json(['code'=>-1,'非法请求','data'=>[]]);
        $id = $this->request->post('id',0,'intval');
        $rt = \app\admin\model\Menus::where('id','=',$id)->delete();
        if($rt) return json(['code'=>0,'message'=>'删除成功','data'=>[]]);
    }


}